package com.oscar.ehome.view.pageview.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.C;
import com.oscar.ehome.bean.pagebean.StudyBean;
import com.oscar.ehome.bean.setbean.LearnHistoryBean;
import com.oscar.ehome.provider.gildeimage.GildeUtils;
import com.xiao.nicevideoplayer.LogUtil;

import java.util.List;

/**
 * Created by oscar
 * author: MengWanhe
 * date: On 2017/7/10
 * 学习列表
 */

public class StudyAdapter extends BaseAdapter {

    private List<StudyBean.ListBean> list;
    List<LearnHistoryBean.LearnListBean> lists;
    private LayoutInflater mInflater;
    private Context mContext = null;
    int bean = 0;
    int size = 0;

    public StudyAdapter(Context context, List<StudyBean.ListBean> list, int bean) {
        this.mContext = context;
        this.list = list;
        this.bean = bean;
        mInflater = LayoutInflater.from(context);
    }

    public StudyAdapter(Context context, List<LearnHistoryBean.LearnListBean> lists) {
        this.mContext = context;
        this.lists = lists;
        mInflater = LayoutInflater.from(context);
    }

    public void setLists(List<StudyBean.ListBean> list, int bean) {
        this.list = list;
        this.bean = bean;
        notifyDataSetChanged();
    }

    public void setLists(List<LearnHistoryBean.LearnListBean> lists) {
        this.lists = lists;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (bean == 1) {
            size = list.size();
        } else {
            size = lists.size();
        }
        return size;
    }

    @Override
    public Object getItem(int position) {
        if (bean == 1) {
            return list.get(position);
        }
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_study, null);
            holder = new ViewHolder();
            holder.study_title = (TextView) convertView.findViewById(R.id.study_title);
            holder.study_stick = (TextView) convertView.findViewById(R.id.study_stick);
            holder.study_img = (ImageView) convertView.findViewById(R.id.study_img);
            holder.study_time = (TextView) convertView.findViewById(R.id.study_time);
            convertView.setTag(holder); //
        } else {
            holder = (ViewHolder) convertView.getTag(); //
        }

        switch (bean) {
            case 0:
                if (lists.size() > 0) {
                    if (lists.get(position) != null) {
                        holder.study_title.setText(lists.get(position).getLearn().getTitle());
                        holder.study_time.setText(lists.get(position).getUpdateDate());
                        String importurl = C.qiniu_ip + lists.get(position).getLearn().getSmallImg();
//                        GildeUtils.imgLoadPic(mContext, importurl, holder.study_img, "net");
                        String learnType = lists.get(position).getLearn().getLearnType();
                        if ("video/mp4".equals(learnType)) {//视频
                            GildeUtils.loadUrlImageshipin(importurl, holder.study_img, mContext);
                        } else if ("application/pdf".equals(learnType)) {//PDF
                            GildeUtils.loadUrlImagepdf(importurl, holder.study_img, mContext);
                        } else if ("audio/mp3".equals(learnType)) {//音频
                            GildeUtils.loadUrlImageyinpin(importurl, holder.study_img, mContext);
                        }else if ("application/vnd.ms-excel".equals(learnType) || "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(learnType)) {//音频
                            GildeUtils.loadUrlImageexcel(importurl, holder.study_img, mContext);
                        }

                        if ("1".equals(lists.get(position).getLearn().getIsSticky())) {
                            holder.study_stick.setVisibility(View.VISIBLE);
                        } else {
                            holder.study_stick.setVisibility(View.GONE);
                        }
                    }
                }
                break;
            case 1:
                if (list.size() > 0) {
                    if (list.get(position) != null) {
                        holder.study_title.setText(list.get(position).getTitle());
                        holder.study_time.setText(list.get(position).getUpdateDate());
                        String importurl = C.qiniu_ip + list.get(position).getSmallImg();
//                        GildeUtils.imgLoadPic(mContext, importurl, holder.study_img, "net");
                        String learnType = list.get(position).getLearnType();
                        if ("video/mp4".equals(learnType)) {//视频
                            GildeUtils.loadUrlImageshipin(importurl, holder.study_img, mContext);
                        } else if ("application/pdf".equals(learnType)) {//PDF
                            GildeUtils.loadUrlImagepdf(importurl, holder.study_img, mContext);
                        } else if ("audio/mp3".equals(learnType)) {//音频
                            GildeUtils.loadUrlImageyinpin(importurl, holder.study_img, mContext);
                        }

                        if ("1".equals(list.get(position).getIsSticky())) {
                            holder.study_stick.setVisibility(View.VISIBLE);
                        } else {
                            holder.study_stick.setVisibility(View.GONE);
                        }
                    }
                }
                break;
        }
        return convertView;
    }

    class ViewHolder {
        TextView study_title;//标题
        ImageView study_img;//图片
        TextView study_time;//时间
        TextView study_stick;//
    }

}
